package main

import api.AccountApi
import interceptor.LogInterceptor
import main.Gf2BbsRetrofit.Companion.API
import main.Gf2BbsRetrofit.Companion.MAPPER
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.jackson.JacksonConverterFactory
import java.util.concurrent.TimeUnit

/**
 * @author BX-Gin
 * @since 2024/11/19 09:34
 * @param debug 是否为debug模式（打印日志）
 * @property client OkHttpClient
 * @property retrofit Retrofit!
 * @property accountApi 账号相关API
 * @constructor
 */
class LoginRetrofit(private val debug: Boolean = false) {
    private val client = OkHttpClient.Builder()
        .callTimeout(30, TimeUnit.SECONDS)
        .connectTimeout(30, TimeUnit.SECONDS)
        .readTimeout(30, TimeUnit.SECONDS)
        .also { if (debug) it.addInterceptor(LogInterceptor()) }
        .build()

    private val retrofit = Retrofit.Builder()
        .client(client)
        .baseUrl(API)
        .addConverterFactory(JacksonConverterFactory.create(MAPPER))
        .build()

    val accountApi: AccountApi = retrofit.create(AccountApi::class.java)
}